// pages/help/refund/refund.js
let app = getApp()
let request = require("../../../utils/request.js")
let upload = require("../../../utils/upload.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: app.globalData.screenHeight,
    orderVal: '',
    reasonVal: '',
    tempFilePaths: [],
    payOrderImg: [],
    exampleImg: ['https://file.zmzncs.com/file/WeChat/wx_pay.png', 'https://file.zmzncs.com/file/WeChat/zfb_pay.png'],
    exampleIndex: 0,
    exampleDialog: false,
    verifyLoginDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let token = wx.getStorageSync('lmtc_token');
    if (!token) {
      this.setData({
        verifyLoginDialog: true
      })
    }
  },
  getOrderVal(e) {
    this.setData({
      orderVal: e.detail.value
    })
  },
  refundMessage(e) {
    this.setData({
      reasonVal: e.detail.value
    })
  },
  showExample() {
    wx.previewImage({
      urls: this.data.exampleImg,
    })
  },
  chooseImg() {
    let that = this
    wx.chooseImage({
      count: 1,
      sourceType: ['album'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        let url = '/upload/upload-batch'
        upload.file(url, tempFilePaths[0], 'files')
          .then(res => {
            let data = JSON.parse(res.data)
            if (data.code == 200) {
              let arrUrl = []
              for (var i = 0; i < data.data.length; i++) {
                arrUrl.push(data.data[i].url)
              }
              that.setData({
                payOrderImg: arrUrl
              })
            }
          })
        that.setData({
          tempFilePaths
        })
      }
    })
  },
  submitMsg() {
    let that = this
    let url = '/order/order-refund/applyRefund'
    if (!this.data.orderVal) {
      wx.showToast({
        title: '请填写订单号',
        icon: 'none'
      })
      return
    }
    if (!this.data.reasonVal) {
      wx.showToast({
        title: '请填写退款原因',
        icon: 'none'
      })
      return
    }
    if (this.data.payOrderImg.length <= 0) {
      wx.showToast({
        title: '请选择支付截图',
        icon: 'none'
      })
      return
    }
    let parmas = {
      imgUrlList: this.data.payOrderImg,
      orderCode: this.data.orderVal,
      reason: this.data.reasonVal
    }
    request.http(url, 'POST', parmas, 0)
      .then(res => {
        if (res.data.code == 200) {
          let data = res.data.data
          wx.showModal({
            content: data.msg,
            showCancel: false,
            success(res) {
              if (res.confirm && data.status) {
                wx.redirectTo({
                  url: '/pages/help/refundRecord/refundRecord',
                })
              }
            }
          })
          if (res.data.data.status) {
            that.setData({
              orderVal: '',
              reasonVal: '',
              tempFilePaths: [],
              payOrderImg: []
            })
          }
        }
      })
  },
  currentIndex(e) {
    this.setData({
      exampleIndex: e.detail.current
    })
  },
  showExampleDialog() {
    this.setData({
      exampleDialog: true
    })
  },
  hideExampleDialog() {
    this.setData({
      exampleDialog: false
    })
  },
  // 粘贴订单号
  pasteOrder() {
    let that = this
    wx.getClipboardData({
      success: (res) => {
        that.setData({
          orderVal: res.data
        })
      },
    })

  }
})